package com.digdes.calendar.gwt.client.widget;

import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;

public class DayWidget extends IntervalCalendar implements ClickListener {
    // private NavBar navbar = new NavBar();
    //public Grid titleGrid = new Grid(1, 2);
    //private DockPanel dayHeader = new DockPanel();

    private static DayWidget instance;

    public static DayWidget Instance() {
        if (instance == null) {
            instance = new DayWidget();
        }
        return instance;
    }

    private DayWidget() {
        super();
        drawCore();
    }

    public void setDataEventsPanel() {
        eventsPanel.setData(DateUtil.getDay(), DateUtil.getMonth(), DateUtil.getYear(), eventDTOList);
    }

    @Override
    public void drawCore() {
        for (int i = 0; i < Data.HOURS_TITLE.length; i++) {
            createHourColumn(i);
            createDaysColumn(i, 1);
        }
        drawCalendar();
    }

    @Override
    public void getEvents() {
        String s = DateUtil.getDate().getDate() + "." + (DateUtil.getMonth() + 1) + "." + DateUtil.getYear();
        calendarService.getEvents(s, new AsyncCallback<ArrayList<EventDTOImpl>>() {
            public void onFailure(Throwable caught) {
            }

            public void onSuccess(ArrayList<EventDTOImpl> result) {
                eventDTOList.addAll(result);
                setEventInCellDay();
            }
        });
        s = null;
    }

    @Override
    public void setDataHeader() {
        //titleGrid.setText(0, 0, DateUtil.getDay() + " " + Data.OTHER_MONTH[DateUtil.getMonth()] + ", ");
        calendarHeader.setText(0, 0, Data.DAYS_FULL_TITLE[DateUtil.getDate().getDay()]);
        if (DateUtil.getDay() == DateUtil.getNowDay())
        calendarHeader.setText(0, 0,(Data.DAYS_FULL_TITLE[DateUtil.getDate().getDay()] +" (сегодня)"));
    }

    int width = 927;
    int moveLeft = 255;
    int moveTop = 0;
    int left = 0;

    /* public void setEventInCellDay() {
        clearDayWidget();
        setTitleGridData();
        setDataEventsPanel();


        for (CellGridDay c : gridInCellList) {
            int countEvent = 0;
            left = 0;
            if (date.getDay() == date.getNowDay()) {
                c.addStyleName("today");
            } else c.removeStyleName("today");

            for (EventDTOImpl eC : eventDTOList) {
                if ((eC.getDateStart().getDate() == date.getDay()) &&
                        (eC.getDateStart().getMonth() == date.getMonth()))
                    if (eC.getDateStart().getDate() == eC.getDateFinish().getDate()) {
                        if (eC.getDateStart().getHours() == c.getHour()) {
                            countEvent++;
                        }
                    }
            }
            for (EventDTOImpl e : eventDTOList) {
                if (e.getDateStart().getDate() == e.getDateFinish().getDate()) {
                    if (e.getDateStart().getHours() == c.getHour()) {
                        EventLabel panel = new EventLabel(e);
                        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateStart().getMinutes()) +
                                " - " + e.getDateFinish().getHours() + ":" +
                                date.addZeroInMinutes(e.getDateFinish().getMinutes());
                        panel.setStyleName("EventLabelDay");
                        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
                        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
                        panel.setHeight(String.valueOf((e.getDateFinish().getHours() -
                                e.getDateStart().getHours()) * 50 + minutes - 3));
                        panel.setWidth(String.valueOf((width / countEvent) - 7));
                        panel.setText(title);
                        panel.setTitle(title);
                        panel.addClickListener(this);
                        aPanel.add(panel, (c.getAbsoluteLeft() - moveLeft +
                                c.getLeft()),
                                minutesStart + c.getAbsoluteTop() - moveTop);

                        left += (width / countEvent);
                        c.setLeft(left);
                        eventLabels.add(panel);

                    }
                }
            }
            c.setLeft(0);
        }
    }*/

    public void setEventInCellDay() {
        moveTop = gridInCellList.get(0).getAbsoluteTop();
        setDataHeader();
        setDataEventsPanel();
        for (CellGrid c : gridInCellList) {
            left = 0;
            c.getEventDTOList().clear();
            if (DateUtil.getDay() == DateUtil.getNowDay()) {
                c.addStyleName("today");
            } else {
                c.removeStyleName("today");
            }

            int countEvent = 0;
            for (EventDTOImpl eventDTO : eventDTOList) {
                if (eventDTO.getDateStart().getDate() == eventDTO.getDateFinish().getDate()
                        && eventDTO.getDateStart().getDate() == DateUtil.getDay()
                        && eventDTO.getDateStart().getMonth() == DateUtil.getMonth()
                        && (eventDTO.getDateStart().getYear() + 1900) == DateUtil.getYear()) {
                    if (eventDTO.getDateStart().getHours() == c.getHour()) {
                        countEvent++;
                        c.getEventDTOList().add(eventDTO);
                    }
                }
            }
            for (EventDTOImpl e : c.getEventDTOList()) {
                drawEvent(c, e, countEvent);
            }
            c.setLeft(0);
        }
    }

    private void drawEvent(CellGrid c, EventDTOImpl e, int countEvent) {
        label = createLabel(e);
        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                DateUtil.addZeroInMinutes(e.getDateStart().getMinutes()) +
                " - " + e.getDateFinish().getHours() + ":" +
                DateUtil.addZeroInMinutes(e.getDateFinish().getMinutes());
        label.setStyleName("EventLabelDay");
        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
        label.setHeight(String.valueOf((e.getDateFinish().getHours() -
                e.getDateStart().getHours()) * 50 + minutes - 3));
        label.setWidth(String.valueOf((width / countEvent) - 7));
        label.setText(title);
        label.setTitle(title);
        aPanel.add(label, (c.getAbsoluteLeft() - moveLeft +
                c.getLeft()),
                minutesStart + c.getAbsoluteTop() - moveTop);

        left += (width / countEvent);
        c.setLeft(left);
        eventLabels.add(label);
        /*  EventLabel label = new EventLabel(e);
        String title = e.getNameEvent() + "\n" + e.getDateStart().getHours() + ":" +
                date.addZeroInMinutes(e.getDateStart().getMinutes()) +
                " - " + e.getDateFinish().getHours() + ":" +
                date.addZeroInMinutes(e.getDateFinish().getMinutes());
        label.setStyleName("EventLabelDay");
        float minutes = (float) ((e.getDateFinish().getMinutes() - e.getDateStart().getMinutes()) * 0.8);
        int minutesStart = (int) (e.getDateStart().getMinutes() * 0.8);
        label.setHeight(String.valueOf((e.getDateFinish().getHours() -
                e.getDateStart().getHours()) * 50 + minutes - 3));
        label.setWidth(String.valueOf((Util.WIDTH_WEEK_CELL / countEvent) - 6));
        label.setText(title);
        label.setTitle(title);
        label.addClickListener(this);
        aPanel.add(label, c.getAbsoluteLeft() - moveLeft + c.getLeft(),
                minutesStart + c.getAbsoluteTop() - moveTop);
        left += (Util.WIDTH_WEEK_CELL / countEvent);
        c.setLeft(left);
        eventLabels.add(label);*/

        title = null;
    }

    private void deleteEvent(final EventLabel label) {
        eventDBox.deleteEvent(label, eventLabels);
        for (CellGrid c : gridInCellList) {
            c.getEventDTOList().remove(label.getEventDTO());
        }
    }
/*
    private class NavBar extends Composite {

        public DockPanel bar = new DockPanel();
        public HTML titleYear = new HTML();
        public Grid gridTitleYear = new Grid(1, 2);

        public NavBar() {

            setWidget(bar);
            bar.setStyleName("navbar");
            titleYear.setStyleName("header");
            bar.add(gridTitleYear, DockPanel.CENTER);
            bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
            bar.setCellHorizontalAlignment(gridTitleYear,
                    HasAlignment.ALIGN_CENTER);
            bar.setCellVerticalAlignment(gridTitleYear,
                    HasAlignment.ALIGN_MIDDLE);
            bar.setCellWidth(gridTitleYear, "100%");
        }
    }*/

    @Override
    protected Widget getNavBar() {
        return new NavBar();
    }

    protected class NavBar extends Composite implements ClickListener {

        public DockPanel bar = new DockPanel();
        public Button prevDay = new Button("&lt;", this);
        // public Button prevYear = new Button("«", this);
        //public Button nextYear = new Button("»", this);
        public Button nextDay = new Button("&gt;", this);
        public HTML titleMonth = new HTML();

        public NavBar() {
            setWidget(bar);
            bar.setStyleName("navbar");
            titleMonth.setStyleName("header");
            titleMonth.setText(DateUtil.getDay() + " " + Data.OTHER_MONTH[DateUtil.getMonth()] + " " +
                    DateUtil.getYear());
            HorizontalPanel prevButtons = new HorizontalPanel();
            prevButtons.add(prevDay);

            HorizontalPanel nextButtons = new HorizontalPanel();
            nextButtons.add(nextDay);

            Grid gridTitleMonth = new Grid(1, 1);

            gridTitleMonth.setWidget(0, 0, titleMonth);

            bar.add(prevButtons, DockPanel.WEST);
            bar.setCellHorizontalAlignment(prevButtons, DockPanel.ALIGN_LEFT);
            bar.add(nextButtons, DockPanel.EAST);
            bar.setCellHorizontalAlignment(nextButtons, DockPanel.ALIGN_RIGHT);
            bar.add(gridTitleMonth, DockPanel.CENTER);
            bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
            bar.setCellHorizontalAlignment(gridTitleMonth,
                    HasAlignment.ALIGN_CENTER);
            bar.setCellVerticalAlignment(gridTitleMonth,
                    HasAlignment.ALIGN_MIDDLE);
            bar.setCellWidth(gridTitleMonth, "100%");
        }

        public void onClick(Widget sender) {
            if (sender == prevDay) {
                DateUtil.prevDay();
                titleMonth.setText(DateUtil.getDay() + " " + Data.OTHER_MONTH[DateUtil.getMonth()] + " " +
                        DateUtil.getYear());
            } else if (sender == nextDay) {
                DateUtil.nextDay();
                titleMonth.setText(DateUtil.getDay() + " " + Data.OTHER_MONTH[DateUtil.getMonth()] + " " +
                        DateUtil.getYear());
            }
            drawCalendar();
        }
    }
}
